$(function() {
	var tiMuGrid = $("#TiMuGrid");
	var timuGridAction = $("#timuGridAction");
	tiMuGrid
			.datagrid({
				url : 'timu/list',
				fit : true,
				border : false,
				rownumbers : true,
				pagination : true, // 是否显示分页栏
				fitColumns : false,
				columns : [ [
						{
							field : 'phase_name',
							title : '所属阶段',
							align : 'center',
							width : 60
						},
						{
							field : 'chapter_name',
							title : '所属章节',
							align : 'center',
							width : 150
						},
						{
							field : 'title_type',
							title : '题目类型',
							align : 'center',
							width : 60
						},
						{
							field : 'title',
							title : '题目',
							width : 400,
							formatter : function(value) {
								return "<span title='" + value + "'>" + value
										+ "</span>";
							}
						},
						{
							field : 'optiona',
							title : 'A选项',
							width : 150,
							formatter : function(value) {
								if (value==null||value=='') {
									return "";
								}else{
									return "<span title='" + value + "'>" + value
									+ "</span>";
								}
							}
						},
						{
							field : 'optionb',
							title : 'B选项',
							width : 150,
							formatter : function(value) {
								if (value==null||value=='') {
									return "";
								}else{
									return "<span title='" + value + "'>" + value
									+ "</span>";
								}
							}
						},
						{
							field : 'optionc',
							title : 'C选项',
							width : 150,
							formatter : function(value) {
								if (value==null||value=='') {
									return "";
								}else{
									return "<span title='" + value + "'>" + value
									+ "</span>";
								}
							}
						},
						{
							field : 'optiond',
							title : 'D选项',
							width : 150,
							formatter : function(value) {
								if (value==null||value=='') {
									return "";
								}else{
									return "<span title='" + value + "'>" + value
									+ "</span>";
								}
							}
						},
						{
							field : 'title_answer',
							title : '答案',
							align : 'center',
							width : 50,
							formatter : function(value) {
								if (value==null||value=='') {
									return "<span  style='color:red;'  style='color:red;' title='暂无'>暂无</span>";
								}else{
									return "<span title='" + value + "'>" + value
									+ "</span>";
								}
							}
						},
						{
							field : 'images_path',
							title : '效果图',
							align : 'center',
							width : 90,
							formatter : function(value, row, index) {
								if (value != null && value != "") {
									var bts = [];
									bts
											.push('<a style="color:black;text-decoration: none;" href="'
													+ 'upload/'
													+ row.images_path
													+ '"target="_blank" data-index="'
													+ index
													+ '" class="ctr preview">预览</a>');
									bts
											.push('<a style="color:black;text-decoration: none;" href="'
													+ 'download/'
													+ row.images_id
													+ '" data-index="'
													+ index
													+ '" class="ctr download">下载</a>');
									return bts.join('');
								} else {
									return "没有上传";
								}

							}
						},
						{
							field : 'title_time',
							title : '出题时间',
							align : 'center',
							width : 130,
							formatter : function(value, row, index) {
								var c = new Date(value);// 传入一个时间戳
								c.toLocaleString();// 返回本地时间
								var d = new Date(c);
								var formatIt = d.getFullYear() + '-'
										+ (d.getMonth() + 1) + '-'
										+ d.getDate() + ' ' + d.getHours()
										+ ':' + d.getMinutes() + ':'
										+ d.getSeconds();
								return formatIt;
							}
						},
						{
							field : 'ac',
							title : '操作',
							align : 'center',
							width : 150,
							formatter : function(value, row, index) {
					/*			var bts = [];
								bts.push('<a data-index="' + index
										+ '" class="ctr edit">编辑</a>');
								bts.push('<a data-id="' + row.id
										+ '" data-index="' + index
										+ '" class="ctr delete">删除</a>');*/
								return timuGridAction.children("a.ctr").attr('data-id',
										row.id).attr('data-index', index).end().html();
							}
						} ] ],
				toolbar :"#timuGridToolbar"
			});
	var phase = $("#phase");
	var chapter = $("#chapter");
	var titleType = $("#titleType");
	// 给检索按钮添加事件
	$("#titleSearch").on('click', function() {
		tiMuGrid.datagrid("load", {
			titleId : phase.val(),
			title : chapter.val(),
			titleType : titleType.val()
		});
	});
	var timuForm = $("#timuForm");
	$("#titleReset").on('click', function() {
		tiMuGrid.datagrid("load", {})
	});

	var gridPanel = tiMuGrid.datagrid("getPanel");
	gridPanel.on("click", "a.edit", function() {
		var index = this.dataset.index;
		var rows = tiMuGrid.datagrid("getRows");
		var id = rows[index].title_id;
		titleForm(id);
	}).on("click", "a.delete", function() {
		var index = this.dataset.index;
		var rows = tiMuGrid.datagrid("getRows");
		var id = rows[index].title_id;
		$.messager.confirm("提示", "你确定要删除此数据？", function(r) {
			if (r) {
				$.get("timu/delete/" + id, function(rs) {
					if (rs.success) {
						tiMuGrid.datagrid("reload");
					} else {
						$.message.alert("提示", rs.message)
					}
				});
			}
		});
	}).on("click", "a.create", function() {
		titleForm();
	})

	function titleForm(id) {
		var dialog = $("<div/>").dialog({
			href : 'jiaoxue/jx-add-timu' + (id ? ("/" + id) : ""),
			title : '添加题目',
			height : '540',
			width : '610',
			modal : true,
			onClose : function() {
				$(this).dialog("destroy");
			},
			buttons : [ {
				iconCls : 'icon-save',
				text : '添加',
				handler : function() {
					$("#addTitleForm").form('submit', {
						url : 'timu/save',
						data : $("#addTitleForm").serialize(),
						success : function(success) {
							if (success) {
								// 刷新数据表格
								tiMuGrid.datagrid("reload");
								// 关闭窗口
								dialog.dialog("close");
								$.messager.alert("提示", "上传成功！");
							} else {
								$.messager.alert("提示", success.message);
							}
						}
					});
				}
			}, {
				iconCls : 'icon-clear',
				text : '取消',
				handler : function() {
					dialog.dialog("close");
				}
			} ]
		})
	}
});